/**  
 * All rights Reserved, Designed By www.woniuxy.com
 * @Title:  Account.java   
 * @Package com.woniu.domain   
 * @Description:    Description  
 * @author: 蜗牛学院  
 * @date:   2019年10月14日 下午8:51:46
 * @user:   Administrator  
 * @version V1.0 
 * @Copyright: 2019 www.woniuxy.com Inc. All rights reserved. 
 * 注意：本内容仅限于成都蜗牛创想科技有限公司，禁止外泄以及用于其他的商业目
 */ 
package com.moon.config;

import java.util.HashSet;
import java.util.Set;

import org.springframework.cache.CacheManager;
import org.springframework.cache.support.SimpleCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;

import com.moon.utils.RedisCache;

import redis.clients.jedis.JedisPoolConfig;

/**
 * @author Administrator
 *
 */
@Configuration
public class RedisConfig {
	//连接池配置
	@Bean
	public JedisPoolConfig jedisPoolConfig() {
		JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
		jedisPoolConfig.setMaxIdle(400);
		jedisPoolConfig.setMaxTotal(6000);
		jedisPoolConfig.setMaxWaitMillis(1000);
		jedisPoolConfig.setTestOnBorrow(true);
		return jedisPoolConfig;
	}
	//数据库连接配置
	@Bean
	public JedisConnectionFactory jedisConnectionFactory(JedisPoolConfig jedisPoolConfig) {
		JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
		jedisConnectionFactory.setHostName("127.0.0.1");
		jedisConnectionFactory.setPort(6379);
		jedisConnectionFactory.setTimeout(100000);
		jedisConnectionFactory.setPoolConfig(jedisPoolConfig);
		jedisConnectionFactory.setUsePool(true);
		return jedisConnectionFactory;
	}
	//非关系型数据库redis配置
	@Bean//(name="redisTemplate")
	public RedisTemplate redisTemplate(JedisConnectionFactory jesdisConnectionFactory) {
		RedisTemplate redisTemplate = new RedisTemplate();
		redisTemplate.setConnectionFactory(jesdisConnectionFactory);
		redisTemplate.setEnableTransactionSupport(true);
		return redisTemplate;
	}
	@Bean
	public StringRedisTemplate stringRedisTemplate(JedisConnectionFactory jedisConnectionFactory) {
		StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
		stringRedisTemplate.setConnectionFactory(jedisConnectionFactory);
		stringRedisTemplate.setEnableTransactionSupport(true);
		return stringRedisTemplate;
	}
	//缓存管理器配置
	@Bean
	public SimpleCacheManager cacheManager(RedisTemplate redisTemplate) {
		SimpleCacheManager cacheManager = new SimpleCacheManager();
		Set<RedisCache> set = new HashSet<RedisCache>();
		RedisCache redisCache = new RedisCache();
		redisCache.setRedisTemplate(redisTemplate);
		redisCache.setName("cache0");
		set.add(redisCache);
		cacheManager.setCaches(set);
		return cacheManager;
	}
}
